package com.pur.zsms.systemsetting.datadictionary.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.pur.zsms.common.dao.GenericDao;
import com.pur.zsms.common.dao.JpaTemplate;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.systemsetting.datadictionary.dao.IDataDictionaryDao;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;

@Repository("dataDictionaryDao")
public class DataDictionaryDaoImpl extends GenericDao implements IDataDictionaryDao {

	public PaginationSupport findCodeList(PageController pageController, CodeVo codevo, String codeTypeId) throws GenericException {
		// TODO Auto-generated method stub
		StringBuffer ql = new StringBuffer();
		ql.append(" select * ");
		ql.append("   from t_code");
		// if(codeTypeId!=null&&!codeTypeId.equals("")){
		ql.append(" where code_type_id='" + codeTypeId + "' order by code_oder");
		// }
		return this.findByNativeQuery(ql.toString(), Code.class, pageController);
	}

	/**
	 * 根据id查寻代码类型
	 * 
	 * @param codeTypeId
	 * @return
	 */
	public List<CodeType> findById(long codeTypeId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		CodeType c = new CodeType();
		StringBuffer ql = new StringBuffer();

		ql.append(" select o ");
		ql.append("   from CodeType o");
		// ql.append("  where o.codeTypeId = '" + codeTypeId + "'");

		/*
		 * CodeType code = this.find(CodeType.class, codeTypeId);
		 * System.out.print(code.getCodeTypeName());
		 */

		List<CodeType> list = template.find(ql.toString(), null);
		return list;
		// return code;
	}

	/**
	 * 根据id查寻代码类型
	 * 
	 * @param codeTypeId
	 * @return
	 */
	public Code findCodeByCodeId(int codeId, String codeTypeId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		CodeType c = new CodeType();
		StringBuffer ql = new StringBuffer();

		ql.append(" select o ");
		ql.append("   from Code o");
		ql.append("  where o.codeId = '" + codeId + "' and o.codeTypeId ='" + codeTypeId + "'");

		List<Code> list = template.find(ql.toString(), null);
		return list.get(0);
		// return code;
	}

	public void addCodetype(CodeType c) throws GenericException {
		/*
		 * JpaTemplate template = this.getTemplate(); StringBuffer ql = new
		 * StringBuffer();
		 * 
		 * ql.append(" insert into t_code_type o values('2','"+c.getCodeTypeName(
		 * )+"','"+c.getDescrib()+"')");
		 * //template.executeNativeUpdate(ql.toString());
		 * template.executeNativeUpdate(ql.toString());
		 */
		// entityManager.persist(c);
		// c.setCodeTypeId(null);
		// entityManager.getTransaction().begin();
		/*
		 * CodeType c1 = new CodeType(); //c.setCodeTypeId(codeTypeId);
		 * c1.setCodeTypeName("11"); c1.setDescrib("11");
		 */
		this.merge(c);
		// entityManager.persist(c1);
		// entityManager.flush();
		// entityManager.getTransaction().commit();
		// entityManager.close();
	}

	public void deleteCode(String phCodeId) throws GenericException {

		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append(" delete Code o where o.phCodeId=" + phCodeId);
		// template.executeNativeUpdate(ql.toString());
		template.executeUpdate(ql.toString());
	}

	public void addCode(Code c) throws GenericException {
		this.merge(c);
	}

	public List<CodeType> findCodeTypeList() {
		JpaTemplate template = this.getTemplate();
		CodeType c = new CodeType();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o ");
		ql.append("   from CodeType o");
		List<CodeType> list = template.find(ql.toString(), null);
		return list;
	}

	@Override
	public String findCodeName(CodeVo codeVo) {
		StringBuffer sql = new StringBuffer();
		/*
		 * sql.append("select o from T_CODE o where CODE_ID='"+codeVo.getCodeId()
		 * +"' and CODE_TYPE_ID='"+codeVo.getCodeTypeId()+"' and IS_ACTIVE=1");
		 */
		/*
		 * sql.append("select o from T_CODE o where CODE_ID='"+codeVo.getCodeId()
		 * +
		 * "' and CODE_TYPE_ID='"+codeVo.getCodeTypeId()+"' and IS_ACTIVE='1'");
		 */
		sql.append("select o from Code o where codeId='" + codeVo.getCodeId() + "' and codeTypeId='" + codeVo.getCodeTypeId() + "' and isActive='1'");
		List<Code> list = this.getTemplate().find(sql.toString(), null);
		Code code = new Code();
		if (list != null && list.size() > 0) {
			code = list.get(0);
		}
		return code.getCodeName();
	}

	@Override
	public String findCodeId(CodeVo codeVo) {
		StringBuffer jpql = new StringBuffer();
		jpql.append("select c from Code c where c.codeName=:codeName and c.codeTypeId=:codeTypeId and c.isActive=1");
		Code code = (Code) entityManager.createQuery(jpql.toString()).setParameter("codeName", codeVo.getCodeName())
				.setParameter("codeTypeId", codeVo.getCodeTypeId()).getResultList().get(0);

		return code.getCodeId();
	}

	public List<Code> findCodeIdList(String codeType) {
		StringBuffer sql = new StringBuffer();
		sql.append("select o from Code o where codeTypeId='" + codeType + "' and isActive='1'");
		List<Code> list = this.getTemplate().find(sql.toString(), null);
		return list;
	}

	public List<Code> findMaterTypeName(int materType) {
		StringBuffer sql = new StringBuffer();
		sql.append("select o from Code o where codeTypeId='mater_type' and isActive='1' and o.codeOder='" + materType + "'");
		List<Code> list = this.getTemplate().find(sql.toString(), null);
		return list;
	}

}
